package imgshare.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import imgshare.common.api.ResultCode;
import imgshare.common.exception.ApiException;
import imgshare.mapper.CollectMapper;
import imgshare.model.Collect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    private CollectMapper collectMapper;
    private static final Log log = LogFactory.get();

    @Override
    public void addCollect(Integer imageId, Integer userId) {
        int rowChanged = collectMapper.insert(imageId, userId);
        if (rowChanged == 0) {
            log.error("collectMapper.insert({}, {})未生效", imageId,userId);
            throw new ApiException(ResultCode.SQL_ERROR);
        }

    }

    @Override
    public List<Integer> getCollectState(Integer userId) {
        return collectMapper.selectCollectedImageIdByUserId(userId);
    }

    @Override
    public Collect findOne(Integer imageId, Integer userId) {
        return collectMapper.selectByKeyPair(imageId,userId);
    }

    @Override
    public void cancelCollect(Collect collect) {
        int rowChanged = collectMapper.delete(collect);
        if (rowChanged == 0) {
            throw new ApiException(ResultCode.SQL_ERROR);
        }
    }
}
